package com.eastfair.imaster.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.code.ExceptionCode;
import com.eastfair.imaster.dao.CheckEmployeeMapper;
import com.eastfair.imaster.dto.EmployeeDTO;
import com.eastfair.imaster.entity.CheckEmployee;
import com.eastfair.imaster.entity.Employee;
import com.eastfair.imaster.enumeration.CheckEmployeeStatusEnum;
import com.eastfair.imaster.service.CheckEmployeeService;
import com.eastfair.imaster.service.EmployeeService;
import com.eastfair.projectcore.api.ProjectCoreServiceFeign;
import com.eastfair.core.snowflake.DefaultUidGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 公司审核用户表
 * </p>
 *
 * @author ligang
 * @date 2021-08-28
 */
@Slf4j
@Service
@DS("#thread.tenant")
public class CheckEmployeeServiceImpl extends SuperServiceImpl<CheckEmployeeMapper, CheckEmployee> implements CheckEmployeeService {
    @Autowired
    ProjectCoreServiceFeign projectCoreServiceFeign;

    @Autowired
    EmployeeService employeeService;


    /**************sharding 分库分表操作 *****************/
    @Override
    public boolean updateByIdOfShard(CheckEmployee entity) {
        UpdateWrapper<CheckEmployee> updateWrapper = new UpdateWrapper<>();
        //可将指定字段更新为null
        updateWrapper.lambda().eq(CheckEmployee::getProjectId, entity.getProjectId()).eq(CheckEmployee::getId, entity.getId());
        entity.setProjectId(null);
        return SqlHelper.retBool(this.getBaseMapper().update(entity, updateWrapper));
    }

    @Override
    protected R<Boolean> handlerSave(CheckEmployee model) {
        //项目id
        model.setProjectId(ContextUtil.getProjectId());
        //雪花ID
        model.setId(DefaultUidGenerator.getUidOfProjectId(model.getProjectId()));
        return R.successDef();
    }

    @Override
    public R addUserIdOfCompany(Long companyId, Long userAccountId, Boolean isCheck, Integer isAdamin) {
        if (isAdamin == null) {
            //为空，则非管理员
            isAdamin = 0;
        }
        if (isCheck) {
            //需要审核
            CheckEmployee checkEmployee = new CheckEmployee();
            checkEmployee.setCompanyId(companyId);
            checkEmployee.setUserAccountId(userAccountId);
            checkEmployee.setIsAdmin(isAdamin);
            checkEmployee.setStatus(CheckEmployeeStatusEnum.CHECKING);
            save(checkEmployee);
            return R.success(checkEmployee);
        } else {
            //不需要审核直接加入
            EmployeeDTO employeeDTO = new EmployeeDTO();
            employeeDTO.setCompanyId(companyId);
            employeeDTO.setUserAccountId(userAccountId);
            employeeDTO.setIsAdmin(isAdamin);
            return employeeService.addUserIdOfCompany(employeeDTO);
        }
    }

    @Override
    public R checkUserIdOfCompany(Long id, Boolean isConsent) {
        List<CheckEmployee> checkEmployeeList = list(null, Arrays.asList(id));
        if (checkEmployeeList != null && !checkEmployeeList.isEmpty()) {
            CheckEmployee checkEmployee = checkEmployeeList.get(0);
            if (CheckEmployeeStatusEnum.CHECK_SUCC == checkEmployee.getStatus()) {
                return R.fail(ExceptionCode.EMPLOYEE_CHECK_FAIL);
            } else if (CheckEmployeeStatusEnum.CHECK_FAIL == checkEmployee.getStatus()) {
                return R.success();
            }
            if (isConsent) {
                //通过
                checkEmployee.setStatus(CheckEmployeeStatusEnum.CHECK_SUCC);
                EmployeeDTO employeeDTO = new EmployeeDTO();
                employeeDTO.setCompanyId(checkEmployee.getCompanyId());
                employeeDTO.setUserAccountId(checkEmployee.getUserAccountId());
                employeeDTO.setIsAdmin(checkEmployee.getIsAdmin());
                employeeService.addUserIdOfCompany(employeeDTO);
            } else {
                //不通过
                checkEmployee.setStatus(CheckEmployeeStatusEnum.CHECK_FAIL);
            }
            updateByIdOfShard(checkEmployee);
        }
        return R.success();
    }

    @Override
    public R cancelUserIdOfCompany(Long id) {
        CheckEmployee checkEmployee = new CheckEmployee();
        checkEmployee.setId(id);
        checkEmployee.setStatus(CheckEmployeeStatusEnum.CHECKING);
        List<CheckEmployee> checkEmployeeList = list(checkEmployee, null);
        if (checkEmployeeList != null && !checkEmployeeList.isEmpty()) {
            //撤销
            checkEmployee = checkEmployeeList.get(0);
            checkEmployee.setIsDeleted(BusinessConstant.YES);
            updateByIdOfShard(checkEmployee);
        }
        return R.success();
    }

    @Override
    public List<CheckEmployee> list(CheckEmployee checkEmployee, List<Long> ids) {
        //查询
        QueryWrapper<CheckEmployee> queryWrapper = new QueryWrapper<>();
        if (checkEmployee != null) {
            if (checkEmployee.getId() != null) {
                queryWrapper.lambda().eq(CheckEmployee::getId, checkEmployee.getId());
            }
            if (checkEmployee.getCompanyId() != null) {
                queryWrapper.lambda().eq(CheckEmployee::getCompanyId, checkEmployee.getCompanyId());
            }
            if (checkEmployee.getUserAccountId() != null) {
                queryWrapper.lambda().eq(CheckEmployee::getUserAccountId, checkEmployee.getUserAccountId());
            }

            if (checkEmployee.getStatus() != null) {
                queryWrapper.lambda().eq(CheckEmployee::getStatus, checkEmployee.getStatus());
            }
        }
        if (ContextUtil.getProjectId() != null) {
            queryWrapper.lambda().eq(CheckEmployee::getProjectId, ContextUtil.getProjectId());
        }
        if (ids != null && !ids.isEmpty()) {
            queryWrapper.lambda().in(CheckEmployee::getId, ids);
        }
        queryWrapper.lambda().eq(CheckEmployee::getIsDeleted, BusinessConstant.DELETE_NO);
        List<CheckEmployee> companyList = list(queryWrapper);
        return companyList;
    }

    @Override
    public List<CheckEmployee> queryCheckEmployeeOfCompany(Long companyId, CheckEmployeeStatusEnum status) {
        CheckEmployee checkEmployee = new CheckEmployee();
        checkEmployee.setCompanyId(companyId);
        if (status != null) {
            checkEmployee.setStatus(status);
        }
        List<CheckEmployee> checkEmployeeList = list(checkEmployee, null);
        return checkEmployeeList;
    }

    @Override
    public R verifyCompanyIsCheck(Long userAccountId) {
        //已经在审核中，只能添加一个公司
        CheckEmployee checkEmployee = new CheckEmployee();
        checkEmployee.setUserAccountId(userAccountId);
        List<CheckEmployee> checkEmployeeList = list(checkEmployee, null);
        if (checkEmployeeList != null && !checkEmployeeList.isEmpty() && CheckEmployeeStatusEnum.CHECKING.eq(checkEmployeeList.get(0).getStatus())) {
            return R.fail(ExceptionCode.ALREADY_CHECK);
        }
        //是否已经是公司员工
        Employee employee = new Employee();
        employee.setUserAccountId(userAccountId);
        List<Employee> employeeList = employeeService.list(employee, null);
        if (employeeList != null && !employeeList.isEmpty()) {
            return R.fail(ExceptionCode.EMPLOYEE_EXIST);
        }
        return R.success();
    }


}
